package in.muton.edushare.pages.coursepages;

import in.muton.edushare.auth.roles.CourseRoleCheck;
import in.muton.edushare.pages.common.PollsStatsPanel;
import in.muton.edushare.poll.panels.PollList;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.basic.Label;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.dao.PollStatsDAO;
import com.edushare.common.exceptions.EduShareRuntimeException;

public class CoursePolls extends CourseBase {

	public CoursePolls(PageParameters params) {
		super(params);

		CourseRoleCheck crc = new CourseRoleCheck(getCourseInstance()
				.getObject(), getLoggedInUser());
		
		if(!crc.isAdmin() && !crc.isTeacher())
			throw new EduShareRuntimeException("Unauthorised access");

		add(CSSPackageResource
				.getHeaderContribution(PollList.class, "Poll.css"));

		add(new Label("heading", "Polls and Feedback"));

		PollStatsDAO ps = new PollStatsDAO(getSessionORMInterface());
		add(new PollsStatsPanel("statsFrag", ps
				.getTotalPolls(getCourseInstance().getObject()), ps
				.getOpenPolls(getCourseInstance().getObject()), ps
				.getClosedPolls(getCourseInstance().getObject()), ps
				.getDraftPolls(getCourseInstance().getObject())));
		add(new PollList("activePanel", getCourseInstance().getObject(),
				this));
	}

	@Override
	protected String getActivePage(PageParameters params) {
		return "CoursePoll";
	}

	@Override
	public String getPageTitle() {
		return getLoggedInUser().getCompleteUserName() + " | "
				+ getCourse().getObject().getCourseName() + " | Polls | "
				+ ApplicationConstants.APPLICATION_NAME;
	}
}
